package com.example.fragload;

import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.fragload.fragments.SecondFragment;
import com.example.fragload.fragments.FirstFragment;
import com.example.fragload.fragments.ThirdFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ReplaceActivity extends AppCompatActivity {

    @BindView(R.id.nav_view)
    BottomNavigationView navView;
    @BindView(R.id.nav_host_fragment)
    FrameLayout contentFragment;

    Fragment currentFragment;
    List<Fragment> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);
        ButterKnife.bind(this);

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
//        ft.addToBackStack(null);
        ft.add(R.id.nav_host_fragment, new FirstFragment());
        ft.commit();

        navView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                switch (menuItem.getItemId()) {
                    case R.id.navigation_user:
                        switchContent( new FirstFragment());
                        return true;
                    case R.id.navigation_room:
                        switchContent(new SecondFragment());
                        return true;
                    case R.id.navigation_setting:
                        switchContent(new ThirdFragment());
                        return true;
//                        return NavigationUI.onNavDestinationSelected(menuItem, navController);
                }
                return false;
            }
        });
    }

    public Fragment getCurrentFragment() {
        Fragment currentFragment = null;
        FragmentManager fragmentManager = getSupportFragmentManager();
        List<Fragment> fragments = fragmentManager.getFragments();
        if (null != fragments) {
            for (Fragment tmp : fragments) {
                if (tmp != null && tmp.isVisible()) {
                    currentFragment = tmp;
                }
            }
        }
        return currentFragment;
    }

    public void switchContent(Fragment to) {
        if (currentFragment != to) {
            currentFragment = to;
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.nav_host_fragment, to);
//            transaction.addToBackStack(null);//会将Fragment添加到回退栈中
            transaction.commitAllowingStateLoss();
        }
    }
}
